package com.jn.zhinongbao.adapter;

import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.example.supportingagriculturaltreasures.fragment.OrderListFragment;
import com.example.supportingagriculturaltreasures.utils.PreferenceManager;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class OrderActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager2 viewPager;
    private OrderPagerAdapter orderPagerAdapter;
    
    private PreferenceManager preferenceManager;
    private int currentUserId;

    // 订单状态常量
    public static final int STATUS_ALL = -1;        // 全部
    public static final int STATUS_PAID = 1;        // 已付款  
    public static final int STATUS_PENDING = 2;     // 待发货
    public static final int STATUS_SHIPPED = 3;     // 已发货
    public static final int STATUS_CONFIRMED = 4;   // 确认收货
    public static final int STATUS_CANCELLED = 5;   // 已取消

    private final String[] tabTitles = {"全部", "已付款", "待发货", "已发货", "确认收货", "已取消"};
    private final int[] statusList = {STATUS_ALL, STATUS_PAID, STATUS_PENDING, STATUS_SHIPPED, STATUS_CONFIRMED, STATUS_CANCELLED};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);

        initViews();
        initData();
        setupViewPager();
    }

    private void initViews() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("我的订单");
        }

        tabLayout = findViewById(R.id.tab_layout);
        viewPager = findViewById(R.id.view_pager);
    }

    private void initData() {
        preferenceManager = new PreferenceManager(this);
        currentUserId = preferenceManager.getCurrentUserId();
        
        if (currentUserId == -1) {
            Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    private void setupViewPager() {
        orderPagerAdapter = new OrderPagerAdapter(this);
        viewPager.setAdapter(orderPagerAdapter);

        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            tab.setText(tabTitles[position]);
        }).attach();
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    private class OrderPagerAdapter extends FragmentStateAdapter {

        public OrderPagerAdapter(FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @Override
        public Fragment createFragment(int position) {
            return OrderListFragment.newInstance(currentUserId, statusList[position]);
        }

        @Override
        public int getItemCount() {
            return tabTitles.length;
        }
    }
} 